using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;

namespace AndroidApp
{
    public class FileLog : ComApp.ILogService
    {
        private static FileLog instance;
        public static FileLog Instance
        {
            get
            {
                return instance ?? (instance = new FileLog());
            }
        }


        private string fileName;
        public string FileName
        {
            get
            {
                if (string.IsNullOrEmpty(fileName))
                {
                    this.fileName = System.IO.Path.Combine(AppPath.Instance.AppDataPath, "FileLog.txt");
                }
                return this.fileName;
            }
        }

        private string fileNameBak;
        public string FileNameBak
        {
            get
            {
                if (string.IsNullOrEmpty(fileNameBak))
                {
                    this.fileNameBak = System.IO.Path.Combine(AppPath.Instance.AppDataPath, "FileLogBak.txt");
                }
                return this.fileNameBak;
            }
        }

        public void Clear()
        {
            if (System.IO.File.Exists(this.FileNameBak))
                System.IO.File.Delete(this.FileNameBak);

            if (System.IO.File.Exists(this.FileName))
            {
                System.IO.File.Copy(this.FileName, this.FileNameBak);
                System.IO.File.Delete(this.FileName);
            }

        }

        public void Log(Exception eception)
        {
            this.Log(eception.GetType() + ":" + eception.Message + System.Environment.NewLine + eception.StackTrace);

            if (eception.InnerException != null)
            {
                var innerException = eception.InnerException;
                this.Log("InnerException:" + innerException.GetType() + ":" + innerException.Message + System.Environment.NewLine + innerException.StackTrace);
            }

        }

        //public void Log(string msg)
        //{
        //    List<string> stringList = null;
        //    var serializer = new System.Xml.Serialization.XmlSerializer(typeof(List<string>));
        //    if (System.IO.File.Exists(this.FileName))
        //    {
        //        using (var stream = new System.IO.FileStream(this.FileName, System.IO.FileMode.Open))
        //        {
        //            stringList = (List<string>)serializer.Deserialize(stream);
        //        }
        //    }
        //    if (stringList == null)
        //        stringList = new List<string>();
        //    stringList.Add(msg);

        //    using (var writer = new System.IO.StreamWriter(this.FileName))
        //    {
        //        serializer.Serialize(writer, stringList);
        //    }
        //}

        private object sync = new object();

        public void Log(string msg)
        {
            lock (sync)
            {
                using (var writer = new System.IO.StreamWriter(this.FileName,true))
                {
                    writer.WriteLine(msg);
                }
            }
        }

    }
}